/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avl.sv.shared.Annotations;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;

/**
 *
 * @author benbryan
 */
public abstract class ROI extends DefaultMutableTreeTableNode implements Serializable, Cloneable {
    public ArrayList<Attributes> attributes;
    public String text;
    public int id, displayId;
    public boolean selected, negativeROA, analyze, highlighted;
    public ArrayList<Integer> selectedPoints;
    
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }

    public ArrayList<Integer> getSelectedPoints() {
        return selectedPoints;
    }

    public void setSelectedPoints(ArrayList<Integer> selectedPoints) {
        this.selectedPoints = selectedPoints;
    }
        
    public abstract int getType();
            
    public Polygon getPolygon(){
        return getPolygon(new AffineTransform());
    }
    public abstract Polygon getPolygon(AffineTransform at);

    abstract public void changePoint(int index, int nx, int ny);
    abstract public void changePoint(int index, double nx, double ny);
    
    public abstract boolean containsPoint(int x, int y);
    public abstract boolean containsPoint(Point p);
    
    public abstract Shape getShape();
    
    @Override
    public String toString() {
        return String.valueOf(id) + ": " + text;
    }
       
    public abstract void addPoint(int x, int y);
    public abstract void addPoint(double x, double y);
    
    @Override
    public void setUserObject(Object userObject) {
        if (String.class.equals(userObject.getClass())){
            String newString = (String) userObject;
            if (newString.contains(":")) {
                newString = newString.substring(newString.lastIndexOf(":") + 1);
            }
            newString = newString.trim();
            text = newString;

        } else {
            super.setUserObject(toString());
        }
    }
    @Override
    public Object getUserObject() {
        return this;
    }   

    @Override
    public Object clone() {
        try {
            ROI r = (ROI) super.clone();
            r.attributes = (ArrayList<Attributes>) attributes.clone();
            return super.clone();
        } catch (CloneNotSupportedException ex) {
            Logger.getLogger(ROI.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    public void paintROI(Graphics2D g, Color color) {

        Shape s = getShape();
        if (s == null){
            return;
        }

        float zFactor = (float) (1 / g.getTransform().getScaleX());
        g.setColor(color);
        g.setStroke(new BasicStroke(5 * zFactor));
        g.draw(s);

        g.setColor(Color.BLACK);
        g.setStroke(new BasicStroke(1 * zFactor));
        g.draw(s);

        g.setStroke(new BasicStroke(3 * zFactor));

        // Fill the new roi
        g.setColor(Color.green);
        Color c = g.getColor();
        g.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 30));
        g.fill(s);

        g.setColor(Color.red);

    }

}
